<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('所属业务员')">
              <a-select v-model="queryParam.clerkId">
                <a-select-option v-for="item in memberList" :key="item.id" :value="item.id">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('客户来源')">
              <a-select v-model="queryParam.customerSource">
                <a-select-option v-for="item in memberSource" :key="item.value" :value="item.value">{{
                  item.title
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('会员信息')">
              <a-input :placeholder="$t('请输入手机号/卡号/姓名')" v-model="queryParam.memberInfo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}<a-icon :type="toggleSearchStatus ? 'up' : 'down'"
              /></a>
            </span>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="clickBatchEdit" v-has="'BisCustomerManageController:edit'">{{$t('批量修改')}}</a-button>
              <a-button type="primary" @click="clickRandomEdit" style="margin-left: 8px" v-has="'BisCustomerManageController:edit'">{{$t('随机分配')}}</a-button>
              <a-button type="primary" icon="download" :loading="exloading" style="margin-left: 8px" @click="handleExportXls($t('客户管理'))" v-has="'BisCustomerManageController:export'"
                >{{$t('导出')}}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{$t('无图片')}}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{$t('无文件')}}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{$t('下载')}}
          </a-button>
        </template>
      </a-table>
    </div>
    <batch-edit-sales-modal ref="modalForm" @ok="modalFormOk"></batch-edit-sales-modal>
    <random-modal ref="randomForm" @ok="modalFormOk"></random-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import BatchEditSalesModal from './modules/BatchEditSalesModal'
import RandomModal from './modules/RandomModal'

export default {
  name: 'MemberManage',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    BatchEditSalesModal,
    RandomModal,
  },
  data() {
    return {
      description: this.$t('客户管理'),
      isorter: {
        column: 'regDate',
        order: 'desc',
      },
      queryParam: {
        clerkId: '',
        customerSource: '',
      },
      // 表头
      columns: [
        {
          title: this.$t('手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'cardNo',
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('会员类型'),
          align: 'center',
          dataIndex: 'memberType_dictText',
        },
        {
          title: this.$t('代币'),
          align: 'right',
          dataIndex: 'coin',
          customRender: (text, r) => {
            return r.coin.toFixed(2);
          },
        },
        {
          title: this.$t('彩票'),
          align: 'right',
          dataIndex: 'lottery',
          customRender: (text, r) => {
            return r.lottery.toFixed(0);
          }
        },
        {
          title: this.$t('积分'),
          align: 'right',
          dataIndex: 'point',
          customRender: (text, r) => {
            return r.point.toFixed(0);
          }
        },
        {
          title: this.$t('注册时间'),
          align: 'center',
          dataIndex: 'regDate',
        },
        {
          title: this.$t('客户来源'),
          align: 'center',
          dataIndex: 'customerSource_dictText',
        },
        {
          title: this.$t('开始管理时间'),
          align: 'center',
          dataIndex: 'manageBegTime',
        },
        {
          title: this.$t('所属业务员'),
          align: 'center',
          dataIndex: 'salesmanName',
        },
      ],
      url: {
        list: '/business/customerManage/list',
        exportXlsUrl: '/business/customerManage/exportXls',
      },
      dictOptions: {},
      memberList: [],
      memberSource: [],
      addList: [],
      delectList: [],
      selectedRowKeys: [],
      selectionRows: [],
      isCustom:true
    }
  },
  created() {
    this.getSales()
    if (this.$route.query.salesmanId) {
      this.queryParam.clerkId = this.$route.query.salesmanId
    }else{
      this.queryParam.clerkId=''
    }
    this.loadData(1)
  },
  watch: {
    $route(to, form) {
      if (this.$route.query.salesmanId) {
        this.queryParam.clerkId = this.$route.query.salesmanId
      }else{
        this.queryParam.clerkId=''
      }
      this.loadData(1)
    },
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      initDictOptions('bis_customer_manage.customer_source').then((res) => {
        if (res.success) {
          this.memberSource = res.result
          this.memberSource.unshift({ value: '', title: this.$t('全部') })
        }
      })
    },
    getSales() {
      let query = {
        pageNo: 1,
        pageSize: 9999,
      }
      getAction('/member/bisSalesman/list', query).then((res) => {
        if (res.success) {
          this.memberList = res.result.records
          this.memberList.unshift({ name: this.$t('全部'), id: '' }, { name: this.$t('未分配'), id: '0' })
        }
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.addList = []
      this.delectList = []
      selectionRows.forEach((row) => {
        this.addList.push(row.memberId)
        this.delectList.push(row.id)
      })
    },
    clickBatchEdit() {
      if (this.addList.length > 0) {
        this.$refs.modalForm.edit(this.addList, this.delectList)
      } else {
        this.$message.warning(this.$t('请先选择要操作的会员'))
      }
    },
    clickRandomEdit() {
      this.$refs.randomForm.edit()
    },
    modalFormOk(){
      this.loadData(this.ipagination.current);
      //清空列表选中
      this.onClearSelected()
      this.addList = []
      this.delectList = []
    },
    searchReset() {
      this.queryParam = {
        clerkId: '',
        customerSource: '',
        memberInfo: '',
      }
      this.loadData(1)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>